package com.sls.nzdapplication.db.utils;

import android.content.Context;

import com.alibaba.fastjson.JSONArray;
import com.sls.nzdapplication.db.DaoManager;
import com.sls.nzdapplication.db.DaoSession;
import com.sls.nzdapplication.db.MeterDao;
import com.sls.nzdapplication.model.Meter;
import com.sls.nzdapplication.app.MySharedPreferences;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MeterUtil {
    private static final boolean DUBUG = true;
    private DaoManager manager;
    private Meter meter;
    private DaoSession daoSession;
    private MySharedPreferences mySharedPreferences;
    private String username = "";

    public MeterUtil(Context context){
        manager = DaoManager.getInstance();
        manager.init(context);
        daoSession = manager.getDaoSession();
        manager.setDebug(DUBUG);
        mySharedPreferences = new MySharedPreferences(context);
        username = mySharedPreferences.getUserValue("username");
    }

    /**
     * 添加数据，如果有重复则覆盖
     */
    public void insertMeter(Meter meter) {
        meter.setCreateBy(username);
        meter.setCreateTime(new Date());
        manager.getDaoSession().insertOrReplace(meter);
    }


    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultMeter(final List<Meter> meters) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (Meter meter : meters) {
                    meter.setCreateBy(username);
                    meter.setCreateTime(new Date());
                    manager.getDaoSession().insertOrReplace(meter);
                }
            }
        });
    }

    /**
     * 添加多条数据，需要开辟新的线程
     */
    public void insertMultMeter2(final List<Meter> meters) {
        manager.getDaoSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (Meter meter : meters) {
                    manager.getDaoSession().insertOrReplace(meter);
                }
            }
        });
    }

    /**
     * 删除数据
     */
    public void deleteMeter(Meter meter) {
        manager.getDaoSession().delete(meter);
    }

    /**
     * 删除全部数据
     */
    public void deleteAll(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 更新数据
     */
    public void updateMeter(Meter meter) {
        meter.setModifyBy(username);
        meter.setModifyTime(new Date());
        manager.getDaoSession().update(meter);
    }

    /**
     * 按照主键返回单条数据
     */
    public Meter listOneMeter(String key) {
        return manager.getDaoSession().load(Meter.class, key);
    }

    /**
     * 按照表箱id获取电表数据
     */
    public List<Meter> queryMetersByBoxId(String id){
        QueryBuilder<Meter> builder = manager.getDaoSession().queryBuilder(Meter.class);
        List<Meter> meterList = builder.where(MeterDao.Properties.BoxId.eq(id)).list();
        return  meterList;
    }

    public Meter queryMeterByBoxIdAndMeterCode(String boxId,String code){
        QueryBuilder<Meter> builder = manager.getDaoSession().queryBuilder(Meter.class);
        Meter meter = builder.where(builder.and(MeterDao.Properties.BoxId.eq(boxId),MeterDao.Properties.MeterCode.eq(code))).unique();
        return meter;
    }

    public Meter queryMeterByMeterCode(String code){
        QueryBuilder<Meter> builder = manager.getDaoSession().queryBuilder(Meter.class);
        Meter meter = builder.where(MeterDao.Properties.MeterCode.eq(code)).unique();
        return meter;
    }

    public Meter queryMeterByBoxIdAndRowAndColumn(String boxId,int row,int column){
        QueryBuilder<Meter> builder = manager.getDaoSession().queryBuilder(Meter.class);
        Meter meter = builder.where(builder.and(MeterDao.Properties.BoxId.eq(boxId),MeterDao.Properties.RowNum.eq(row),MeterDao.Properties.ColumnNum.eq(column))).unique();
        return meter;
    }

    /**
     * 查询全部数据
     */
    public List<Meter> queryAll() {
        return manager.getDaoSession().loadAll(Meter.class);
    }

    public JSONArray queryAllToJson(){
        JSONArray jsonArray = new JSONArray();
        List<Meter> meters = queryAll();
        for(Meter meter :meters){
            jsonArray.add(meter.toJson());
        }
        return jsonArray;
    }

    public void insertByJsArray(JSONArray jsonArray){
        List<Meter> list = new ArrayList<>();
        for(int i = 0;i<jsonArray.size();i++){
            list.add(new Meter(jsonArray.getJSONObject(i)));
        }
        insertMultMeter2(list);
    }
}
